﻿using System;
using System.Linq;
using System.Collections.Generic;
using log4net;
using Matrix.Common.Utility;
using Matrix.MaKaYunShang.Model;
using Matrix.MaKaYunShang.DAL;
using Matrix.MaKaYunShang.Model.Dto;

namespace Matrix.MaKaYunShang.BLL
{
    public partial class ShopCommodityBLL : BaseBLL<ShopCommodityBLL>
    {
        private readonly ILog _fileLog = LogManager.GetLogger(typeof(ShopCommodityBLL));

        #region 默认方法

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(ShopCommodity model)
        {
            return ShopCommodityDAL.Instance.Add(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(ShopCommodity model)
        {
            return ShopCommodityDAL.Instance.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int Id)
        {
            return ShopCommodityDAL.Instance.Delete(Id);
        }

        /// <summary>
        /// 批量删除一批数据
        /// </summary>
        public bool DeleteList(string Idlist)
        {
            return ShopCommodityDAL.Instance.DeleteList(Idlist);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ShopCommodity GetModel(int Id)
        {
            var ds = ShopCommodityDAL.Instance.GetModel(Id);
            return ds == null ? null : ServicesHelper.DataSetToIList<ShopCommodity>(ds).ToList()[0];
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<ShopCommodity> GetModelList(string strWhere = null)
        {
            var ds = ShopCommodityDAL.Instance.GetList(strWhere);
            return ServicesHelper.DataSetToIList<ShopCommodity>(ds).ToList();
        }

        #endregion

        #region 扩展方法

        /// <summary>
        ///     获得分页数据列表
        /// </summary>
        public TableDataViewDto<ViewShopCommodityDto> GetPagingList(int draw, int pageIndex, int pageSize,
            string searchKeyWord, string colOrder, int shopId, bool showSellBtn = false)
        {
            try
            {
                int totalCount = 0;
                var list = ShopCommodityDAL.Instance.GetPagingList(pageIndex, pageSize,
                    searchKeyWord, colOrder, shopId, ref totalCount);
                foreach (ViewShopCommodityDto item in list)
                {
                    if (item.SellingStatus && item.Quantity == 0)
                    {
                        item.CommodityName = item.CommodityName + " <font style=\"color:red\">[*库存为0]</font>";
                    }

                    item.Action = "<a class='btn btn-warning' href='javascript:;' onclick=updateData(" + item.Id +
                                  ");>修改</a> ";
                    if (!item.IsLanmaSold)
                    {
                        item.Action += "<a class='btn btn-danger' href='javascript:;' onclick=deleteData(" + item.Id +
                                       ");>删除</a>";
                    }
                    if (showSellBtn)
                    {
                        item.Action = "<a class='btn " + (item.SellingStatus ? "btn-primary" : "btn-success") +
                                      "' href='javascript:;' onclick=setSellData(" + item.Id +
                                      "," + (item.SellingStatus ? "true" : "false") + ");>" +
                                      (item.SellingStatus ? "下架" : "上架") + "</a> " + item.Action;
                    }
                }

                var returnDataList = new TableDataViewDto<ViewShopCommodityDto>
                {
                    data = list,
                    draw = draw,
                    recordsFiltered = totalCount,
                    recordsTotal = totalCount
                };

                return returnDataList;
            }
            catch (Exception ex)
            {
                _fileLog.Error(" GetPagingList | Exception: " + ex);
                return null;
            }
        }

        /// <summary>
        /// 获取店铺商品售卖列表
        /// </summary>
        /// <param name="shopId"></param>
        /// <param name="cateId"></param>
        /// <param name="sellingStatus">NULL 选取所有 0 下架 1 上架</param>
        /// <returns></returns>
        public List<ShopCommodity> GetShopCommodityList(int shopId, int cateId, int? sellingStatus)
        {
            return ShopCommodityDAL.Instance.GetShopCommodityList(shopId, cateId, sellingStatus);
        }

        /// <summary>
        /// 根据店铺商品ID集合获取店铺商品列表
        /// </summary>
        /// <param name="shopId"></param>
        /// <param name="shopCommodityIds"></param>
        /// <returns></returns>
        public List<ShopCommodity> GetShopCommodityListAppointedIds(int shopId, string shopCommodityIds)
        {
            var arrShopCommodityId = shopCommodityIds.Split(',');
            foreach (var s in arrShopCommodityId)
            {
                int nShopCommodityId;
                if (!int.TryParse(s, out nShopCommodityId))
                {
                    _fileLog.Error(
                        "ShopCommodityBLL | GetShopCommodityListAppointedIds | Invalid ShopCommodity Ids - " +
                        shopCommodityIds);
                    return null;
                }
            }

            return ShopCommodityDAL.Instance.GetShopCommodityListAppointedIds(shopId, shopCommodityIds);
        }

        /// <summary>
        /// 获取店铺商品售卖列表
        /// </summary>
        /// <param name="shopId"></param>
        /// <returns></returns>
        public List<ShopCommodity> GetOneShopAllCommodityList(int shopId)
        {
            return GetModelList(" ShopId = " + shopId);
        }

        /// <summary>
        /// 通过商品ID删除店铺商品关系数据
        /// </summary>
        /// <param name="idlist"></param>
        /// <returns></returns>
        public bool DeleteByLanmaSoldCommodityIds(string idlist)
        {
            return ShopCommodityDAL.Instance.DeleteByLanmaSoldCommodityIds(idlist);
        }

        /// <summary>
        /// 获取对象信息含扩展
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ShopCommodity GetModelWithExtendInfo(int id)
        {
            return ShopCommodityDAL.Instance.GetModelWithExtendInfo(id);
        }

        /// <summary>
        /// 通过店铺ID以及商品ID得到一个对象实体
        /// </summary>
        /// <remarks>2016-07-27 万乐园 修改</remarks>
        public ShopCommodity GetModelByShopIdAndCommodityId(int shopId, int commodityId)
        {
            var ds = ShopCommodityDAL.Instance.GetModelByShopIdAndCommodityId(shopId, commodityId);
            return ds == null ? null : ServicesHelper.DataSetToIList<ShopCommodity>(ds).ToList()[0];
        }

        /// <summary>
        /// 更新售卖商品
        /// </summary>
        public bool UpdateShopCommodityByCommodityIds(string ids, decimal soldPrice, decimal shopCommission, int displaySort)
        {
            return ShopCommodityDAL.Instance.UpdateShopCommodityByCommodityIds(ids, soldPrice, shopCommission, displaySort);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        /// <remarks>2016-7-27 万乐园 新增</remarks>
        public List<ShopCommodity> GetModelListWithOutIsDelete(string strWhere = null)
        {
            var ds = ShopCommodityDAL.Instance.GetListWithOutIsDelete(strWhere);
            return ServicesHelper.DataSetToIList<ShopCommodity>(ds).ToList();
        }

        /// <summary>
        /// 恢复数据（IsDelete字段，同时修改SellingStatus）
        /// </summary>
        /// <remarks>2016-7-27 万乐园 新增</remarks>
        public bool UpdateIsDeleteByCommodityLibId(int commodityLibId)
        {
            return ShopCommodityDAL.Instance.UpdateIsDeleteByCommodityLibId(commodityLibId);
        }

        /// <summary>
        /// 增加多条数据
        /// </summary>
        /// <remarks>2016-7-27 万乐园 新增</remarks>
        public int AddList(List<int> shopIdList, ShopCommodity newModel)
        {
            return ShopCommodityDAL.Instance.AddList(shopIdList, newModel);
        }

        /// <summary>
        /// 根据商品库ID更改店铺商品排序
        /// </summary>
        /// <remarks>2016-8-19 万乐园 新增</remarks>
        public bool UpdateDisplaySortByCommodityLibId(int displaySort, int commodityLibId)
        {
            return ShopCommodityDAL.Instance.UpdateDisplaySortByCommodityLibId(displaySort, commodityLibId);
        }

        /// <summary>
        /// 根据商品库ID集合以及店铺ID获取店铺商品列表
        /// </summary>
        public List<ShopCommodity> GetListAppointedCommofityLibIds(int shopId, string commodityLibIds)
        {
            return ShopCommodityDAL.Instance.GetListAppointedCommofityLibIds(shopId, commodityLibIds);
        }

        /// <summary>
        /// 得到店铺商品信息（含删除）
        /// </summary>
        public ShopCommodity GetShopCommodityInfoIncludeDelete(int shopCommodityId)
        {
            return ShopCommodityDAL.Instance.GetModelInfoIncludeDelete(shopCommodityId);
        }

        #endregion
    }
}